package com.atguigu.exer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//创建线程的方式4:使用线程池
public class ThreadPool {
    public static void main(String[] args) {
        //1.提供指定线程数量的线程池
        ExecutorService service=Executors.newFixedThreadPool(10);
       // service.submit(); //适合适用于Callable
        //2.执行指定线程的操作，需要提供一个实现了Runnable接口或Callable接口实现类对象
        service.execute(new NumberThread1());//适合用于Runnable
        service.execute(new NumberThread2());
        service.shutdown();//关闭连接池
    }
}
class NumberThread1 implements Runnable{
    public void run(){
        for(int i=0;i<100;i++){
            if(i%2==0){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
}
class NumberThread2 implements Runnable{
    public void run(){
        for(int i=0;i<100;i++){
            if(i%2==1){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
}
